<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 伊善齐商店管理系统</title>
    <!-- 网站图标 -->
    <link rel="icon" type="image/svg+xml" href="static/images/favicon.svg">
    <link rel="alternate icon" type="image/x-icon" href="static/images/favicon.ico">
    <!-- 引入本地的 Bootstrap CSS -->
    <link rel="stylesheet" href="static/css/bootstrap.min.css">
    <!-- 引入本地的自定义 CSS -->
    <link rel="stylesheet" href="static/css/styles.css">
    <style>
        body {
            background-color: #f5f7fa;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 100%;
            max-width: 400px;
        }
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .logo-container {
            text-align: center;
            margin-bottom: 20px;
        }
        .logo-container img {
            max-width: 100px;
            max-height: 100px;
            border-radius: 8px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .btn-login {
            width: 100%;
            padding: 10px;
            font-size: 16px;
        }
        .error-message {
            color: #dc3545;
            text-align: center;
            margin-bottom: 15px;
            padding: 10px;
            background-color: #f8d7da;
            border-radius: 4px;
            display: none;
        }
        .loading-spinner {
            display: none;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo-container">
            <img src="static/images/logo.jpeg" alt="Logo" class="img-fluid">
        </div>
        <h2 class="login-title">商店管理系统登录</h2>
        
        <div id="errorMessage" class="error-message"></div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" class="form-control" id="username" name="username" required placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" class="form-control" id="password" name="password" required placeholder="请输入密码">
            </div>
            <button type="submit" class="btn btn-primary btn-login">
                <span id="loginText">登录</span>
                <span id="loginSpinner" class="loading-spinner">
                    <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                    登录中...
                </span>
            </button>
        </form>
        
        <div class="mt-3 text-center text-muted">
            <p>用户名: admin | 密码: admin</p>
        </div>
    </div>

    <!-- jQuery -->
    <script src="static/js/jquery.min.js"></script>
    <!-- Bootstrap JS -->
    <script src="static/js/bootstrap.bundle.min.js"></script>
    
    <script>
        $(document).ready(function() {
            $('#loginForm').on('submit', function(e) {
                e.preventDefault();
                
                // 显示加载状态
                $('#loginText').hide();
                $('#loginSpinner').show();
                $('#errorMessage').hide();
                
                // 获取表单数据
                const username = $('#username').val();
                const password = $('#password').val();
                
                // 发送登录请求
                $.ajax({
                    url: '/login',
                    type: 'POST',
                    data: {
                        username: username,
                        password: password
                    },
                    dataType: 'json',
                    success: function(response) {
                        // 隐藏加载状态
                        $('#loginText').show();
                        $('#loginSpinner').hide();
                        
                        if (response.success) {
                            // 登录成功，跳转到首页
                            window.location.href = '/';
                        } else {
                            // 显示错误信息
                            $('#errorMessage').text(response.message);
                            $('#errorMessage').show();
                        }
                    },
                    error: function() {
                        // 隐藏加载状态
                        $('#loginText').show();
                        $('#loginSpinner').hide();
                        
                        // 显示错误信息
                        $('#errorMessage').text('登录过程中发生错误，请稍后重试');
                        $('#errorMessage').show();
                    }
                });
            });
        });
    </script>
</body>
</html>